﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KeyboardImageViewer
{
	using System.Runtime.InteropServices;
	using System.Windows;

	[Flags]
	public enum EXECUTION_STATE : uint
	{
		ES_SYSTEM_REQUIRED = 0x00000001,
		ES_DISPLAY_REQUIRED = 0x00000002,
		// Legacy flag, should not be used.
		// ES_USER_PRESENT   = 0x00000004,
		ES_CONTINUOUS = 0x80000000,
	}

	public static class SystemDisplayManagement
	{
		[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
		private static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);

		public static void RequireDisplay()
		{
			Application.Current.Dispatcher.BeginInvoke((Action)(() =>
			{
				SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_DISPLAY_REQUIRED);
			}));
		}

		public static void ReleaseDisplay()
		{
			Application.Current.Dispatcher.BeginInvoke((Action)(() =>
			{
				SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
			}));
		}
	}
}
